package com.jzoffer;

public class RemoveDuplicatedNode {
    public static void main(String[] args) {
        // 1->2->3->3->4->4->5
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(3);
        ListNode l5 = new ListNode(4);
        ListNode l6 = new ListNode(4);
        ListNode l7 = new ListNode(7);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        l6.next = l7;
        ListNode listNode = deleteDuplication(l1);
        while (listNode != null){
            System.out.println(listNode.val);
            listNode = listNode.next;
        }

    }

    public static ListNode deleteDuplication(ListNode pHead){
        if(pHead == null || pHead.next == null){
            return pHead;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = pHead;
        ListNode pre = dummy;
        ListNode cur = pHead;

        while (cur != null){
            boolean flag = false;
            while (cur.next != null && cur.val == cur.next.val){
                cur = cur.next;
                flag = true;
            }
            if(flag){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;

        }

        return dummy.next;
    }
}
